package com.uspacex.calculate.controller;

import com.uspacex.calculate.entity.TaskDetail;
import com.uspacex.calculate.service.AsyncService;
import com.uspacex.calculate.service.CalculateTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/task")
public class TaskManageController {


    private static final Logger log = LoggerFactory.getLogger(TaskManageController.class);
    @Autowired
    private com.uspacex.calculate.mapper.taskDetailMapper taskDetailMapper;
    @Autowired
    private CalculateTool calculateTool;
    @Autowired
    private AsyncService asyncService;
    @Autowired
    private com.uspacex.calculate.mapper.taskCalculateMapper taskCalculateMapper;

    // MARK Changed 原写法如找不到对应task会抛异常
    @GetMapping("/findBy{taskId}")
    public Map<String, Object> findById(@PathVariable String taskId) {
        Map<String, Object> map = new HashMap<>();
        TaskDetail taskDetail = taskDetailMapper.findByTaskId(taskId);
        if (taskDetail != null && taskDetail.isShowFlag()){
            map.put("result", taskDetail);
        }
        return map;
    }


    @GetMapping("/findAll")
    public List<TaskDetail> findAll() {
        List<TaskDetail> result = taskDetailMapper.findAll();
        for (int i = 0; i < result.size(); i++) {
            System.out.println(result.get(i).getTaskName() + "判断" + !result.get(i).isShowFlag());

            if (!result.get(i).isShowFlag()) {
                System.out.println(result.get(i).getTaskName() + "不显示");
                result.remove(i);
                i--;

            }
        }
        return result;
    }

    @GetMapping("/findProcess")
    public Double getProcess(String taskId) {
        Double process = taskDetailMapper.findProcess(taskId);
        return process;
    }

//    @GetMapping("/findProcess")
//    public Double isfinished(String taskId) {
//        Double process = taskDetailMapper.findProcess(taskId);
//        return process;
//    }

    // MARK Changed 返回值改为map
    @GetMapping("/{taskId}/output1")
    public Map<String, Object> output1(@PathVariable String taskId, int start, int end) throws IOException {
        TaskDetail taskDetail = taskDetailMapper.findByTaskId(taskId);
        int id = taskDetail.getId();
        String tableName = taskDetail.getTableName();
        String databaseName = taskDetail.getDatabaseName();
        //System.out.println("查到ID：" + id);
        //System.out.println("查到源数据表名称：" + tableName);
        String pri_name = calculateTool.getPRI_Name(databaseName, tableName);
        System.out.println("查到主键名称：" + pri_name);

        // MARK Map改为LinkedHashMap
        List<LinkedHashMap<String, Object>> result = taskDetailMapper.output1(tableName, databaseName, pri_name, id, start, end);

        Map<String, Object> map = new HashMap<>();
        map.put("result", result);
        map.put("columnInfo", taskDetailMapper.showColumns(tableName, databaseName));
        return map;
    }

    @GetMapping("/{taskId}/output2")
    public List<LinkedHashMap<String, Object>> output2(@PathVariable String taskId) throws IOException {
        TaskDetail taskDetail = taskDetailMapper.findByTaskId(taskId);
        int id = taskDetail.getId();
        // MARK Map改为LinkedHashMap
        List<LinkedHashMap<String, Object>> result = taskDetailMapper.output2(id);
        return result;
    }

    @GetMapping("delete{taskId}")
    public boolean deleteTask(@PathVariable String taskId) {
        taskDetailMapper.delete(taskId);
        return true;
    }


}
